<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="static/jquery.min.js"></script>
<script type="text/javascript" src="static/jquery-ui.min.js"></script>
<link rel="stylesheet" href="static/jquery.ui.autocomplete.css" type="text/css" />
<script type="text/javascript">
$(document).ready(function() {

  $("#psh_search").live('keyup', function(event){
  	  		var keycode = (event.keyCode ? event.keyCode : event.which);
  	  		var textInput = $(this).val();
  	  		var pshTest = $('li.ui-menu-item').filter(function() {
  				return $(this).text() == textInput;
			});
	  		if(keycode == '13'){
	  			var subjectTest = $('span.test').filter(function() {
  					return $(this).text() == textInput;
			   });
	   		if(subjectTest.text().length > 0){
	   			alert("Current subject already assigned.");
	   		}
	   		else{
	   			if(pshTest.text().length > 0){
	  					$.ajax({type : "POST",
		  				url : "/indexer/translate", 
		  				success: function(data){
		  					if(data == "pref"){
	  							$("<li class='subject'><span class='subjectValue test' onClick='getDocBySubject(this)'>" +textInput +"</span> <span class='removeSuggested' onClick='remove(this)'>×</span></li>").appendTo("#suggestedSubjects");		  						 
		  					}
		  					else{
	  							$("<li class='subject'><span class='subjectValue test' onClick='getDocBySubject(this)'>" + data +"</span> (<span class='alt test'>" + textInput + "</span>) <span class='removeSuggested' onClick='remove(this)'>×</span></li>").appendTo("#suggestedSubjects");		  						 		  					
		  					}
		  					},  
		  				data : {input: textInput}
		 			});
	  				}
	  			}
	  		}
	  		else{
     			if(textInput.length > 2){
	  				$.ajax({type : "POST",
		  				url : "/indexer/suggest",
		  				dataType: 'json', 
		  				success: function(data){
		  					$("#psh_search").autocomplete({source: data,
		  						minLength: 3,});
		  					},  
		  				data : {input: textInput}
		 			});
				}
			}
  		});

});
</script>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
